//
//  UMinHead.swift
//  hunan_ios_ldnydfuz
//
//  Created by yzd on 2019/5/5.
//  Copyright © 2019 ldnsm. All rights reserved.
//

import UIKit
import CLImagePickerTool
class UMineHead: UIView {
    var uploadTap: ((String) -> Void)? = nil
    var authStatus :Int = 0
    var avatarUrl :String = ""
    private lazy var userView: UIView = {
        let el = UIView()
        el.backgroundColor = UIColor.white
        el.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 90)
        return el
    }()
    
    private lazy var avatar: UIImageView = {
        let el = UIImageView(frame:CGRect(x:0,y:0,width:50,height:50))
        el.contentMode = .scaleAspectFill
        el.layer.cornerRadius = CGFloat(size25)
        el.clipsToBounds = true
        return el
    }()
    
    private lazy var nameLabel: UILabel = {
        let el = UILabel()
        el.textColor = UIColor.colBlack
        el.text = "立即认证"
        el.font = UIFont.font(size: 18, weight: .medium)
        el.textAlignment = NSTextAlignment.left
        return el
    }()
    private lazy var arrow: UIImageView = {
        let el = UIImageView(frame:CGRect(x:0,y:0,width:8,height:14))
        el.contentMode = .scaleAspectFill
        el.image = #imageLiteral(resourceName: "ico-arrow")
        return el
    }()
    private lazy var splitView: UIView = {
        let el = UIView()
        el.backgroundColor = UIColor.background
        el.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 1)
        return el
    }()
    
    init(frame: CGRect,userData:StateModel) {
        super.init(frame: frame)
        let goGesture = UITapGestureRecognizer(target: self, action: #selector(goattest))
        userView.addGestureRecognizer(goGesture)
        userView.isUserInteractionEnabled = true
        let avatarTap = UITapGestureRecognizer(target: self, action: #selector(avatarClick))
        avatar.addGestureRecognizer(avatarTap)
        avatar.isUserInteractionEnabled = true
        
        getUser(data:userData)
        configUI()
    }
    
    @objc func avatarClick(){
        if authStatus != 2 {
            return
        }
        let previewVC = ImagePreviewVC(images: [self.avatarUrl], index: 0)
        self.firstViewController()?.navigationController?.pushViewController(previewVC, animated: true)
    }
    @objc func goattest(){
        let this = self.firstViewController() as! UserViewController
        if authStatus == 1 {
            UIAlertController.showAlert(title: "温馨提醒", btnName: "我知道了", message: "入驻申请还未处理，请耐心等待")
        }
    }
        
        func getUser(data:StateModel)  {
            self.avatarUrl = data.avatarUrl
            self.avatar.kf.setImage(urlString: data.avatarUrl)
            self.authStatus = data.authStatus
            if  data.authStatus == 2 {
                self.nameLabel.text =  data.nickname
            }
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        func configUI() {
            addSubview(userView)
            userView.snp.makeConstraints {
                $0.top.equalToSuperview()
                $0.left.equalTo(0)
                $0.width.equalTo(screenWidth)
                $0.height.equalTo(90)
            }
            userView.addSubview(avatar)
            avatar.snp.makeConstraints {
                $0.size.equalTo(50)
                $0.left.equalTo(size20)
                $0.centerY.equalToSuperview()
            }
            
            userView.addSubview(nameLabel)
            nameLabel.snp.makeConstraints {
                $0.centerY.equalToSuperview()
                $0.left.equalTo(avatar.snp.right).offset(size15)
                $0.right.equalTo(-size40)
            }
            userView.addSubview(arrow)
            arrow.snp.makeConstraints {
                $0.centerY.equalToSuperview()
                $0.width.equalTo(8)
                $0.height.equalTo(14)
                $0.right.equalTo(-size20)
            }
            addSubview(splitView)
            splitView.snp.makeConstraints{
                $0.left.right.equalToSuperview()
                $0.height.equalTo(size10)
                $0.top.equalTo(userView.snp.bottom)
                $0.bottom.lessThanOrEqualToSuperview()
            }
        }
        
}
